package com.ihr360.sharedservice.common.controller;

import cn.irenshi.meta.exception.IrenshiException;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.request.MailRequest;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.mail.dto.MailSettingDto;
import com.ihr360.mail.service.MailSettingService;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/mail")
@DisableResponseAdvice
public class MailSettingController {

    @Autowired
    private MailSettingService mailSeetingService;

    @ResponseBody
    @RequestMapping(value = "/testConnect", method = RequestMethod.POST)
    public ResultInfo testConnect(AuthUserInfo userInfo, @RequestBody MailSettingDto sendMail) {
        if (StringUtils.isEmpty(sendMail.getMailAddress())) {
            throw new IrenshiException("irs.web.exception.error_type13", "邮箱不能为空");
        }
        if (StringUtils.isEmpty(sendMail.getMailSmtp())) {
            throw new IrenshiException("irs.web.exception.error_type14", "发信箱服务器地址不能为空");
        }
        sendMail.setCompanyId(userInfo.getCompanyId());
        return new ResultInfo.Builder<MailSettingDto>().success(mailSeetingService.testConnect(userInfo.getCompanyId(), sendMail));
    }

    @ResponseBody
    @RequestMapping(value = "/saveMail", method = RequestMethod.POST)
    public ResultInfo saveMail(AuthUserInfo userInfo, @RequestBody MailSettingDto sendMail) {
        if (StringUtils.isEmpty(sendMail.getMailAddress())) {
            throw new IrenshiException("irs.web.exception.error_type13", "邮箱不能为空");
        }
        if (StringUtils.isEmpty(sendMail.getMailSmtp())) {
            throw new IrenshiException("irs.web.exception.error_type14", "发信箱服务器地址不能为空");
        }
        sendMail.setCompanyId(userInfo.getCompanyId());
        return new ResultInfo.Builder<MailSettingDto>().success(mailSeetingService.saveMail(userInfo.getCompanyId(), sendMail));
    }

    @ResponseBody
    @RequestMapping(value = "/deleteMail", method = RequestMethod.GET)
    public ResultInfo deleteMail(AuthUserInfo userInfo, @RequestParam String id) {
        return new ResultInfo.Builder<>().success(mailSeetingService.deleteMail(userInfo.getCompanyId(), id));
    }

    @ResponseBody
    @RequestMapping(value = "/findMailDetail", method = RequestMethod.GET)
    public ResultInfo findMailDetail(AuthUserInfo userInfo) {
        return new ResultInfo.Builder<MailSettingDto>().success(mailSeetingService.findMailDetail(userInfo.getCompanyId()));
    }

    @ResponseBody
    @RequestMapping(value = "/sendMail",method = RequestMethod.POST)
    public Boolean sendMail(@RequestBody MailRequest mailRequest){
        return mailSeetingService.sendMail(mailRequest.getCompanyId(), mailRequest.getEmailContent(), mailRequest.getRecipient(), mailRequest.getSubject());
    }
}
